package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.admin.yuejian21.Bean.traindetail;
import com.example.admin.yuejian21.Model.TrainModel;
import com.example.admin.yuejian21.R;
import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class TrainDetailActivity extends AppCompatActivity {


    private ImageView trainPhoto,btn_back;
    private TextView trainIntro,trainNotice,trainplace,traintime,price,trainname;
    private LinearLayout train_zixun,train_baoming;
    private String train_id,address;
    private traindetail traindetail;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);
        initViews();
        initEvents();
        initData();
    }
    private void initViews(){
        trainPhoto = (ImageView) findViewById(R.id.trainPhoto);
        trainIntro = (TextView) findViewById(R.id.trainDetail);
        trainNotice = (TextView) findViewById(R.id.trainNotice);
        trainplace = (TextView) findViewById(R.id.train_place);
        traintime = (TextView) findViewById(R.id.train_time);
        train_zixun = (LinearLayout) findViewById(R.id.train_zixun);
        train_baoming = (LinearLayout) findViewById(R.id.train_baoming);
        price = (TextView) findViewById(R.id.price);
        trainname = (TextView) findViewById(R.id.train_name);
        btn_back = (ImageView) findViewById(R.id.train_back);

    }
    private void initEvents(){
        train_zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TrainDetailActivity.this, "电话", Toast.LENGTH_SHORT).show();
                Intent phone = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + "13342279212");
                phone.setData(data);
                startActivity(phone);
            }
        });
        train_baoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(TrainDetailActivity.this,PlaceOrderActivity.class);
                intent.putExtra("train_id", train_id);
                intent.putExtra("price", traindetail.getPrice());
                startActivity(intent);
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    private void initData(){
        Intent intent = getIntent();
        train_id = intent.getStringExtra("train_id");
        showData();
    }
    private void showData(){
        TrainModel trainModel = new TrainModel();
        Call<traindetail> call = trainModel.gettraininfo(train_id);
        call.enqueue(new Callback<traindetail>() {
            @Override
            public void onResponse(Call<traindetail> call, Response<traindetail> response) {
                traindetail = response.body();
                trainname.setText(traindetail.getName());
                trainIntro.setText(traindetail.getIntro());
                trainNotice.setText(traindetail.getNotice());
                traintime.setText(traindetail.getTraintime());
                trainplace.setText(traindetail.getPlace());
                price.setText("￥"+traindetail.getPrice());
                String address = "http://172.24.10.175/workout/Uploads/"+traindetail.getThumb();
                if (!traindetail.getThumb().equals("")) {
                    Picasso.with(mContext).load(address).into(trainPhoto);
                }
            }

            @Override
            public void onFailure(Call<traindetail> call, Throwable t) {

            }
        });
    }
}